<template>
  <el-card>
    <el-form :inline="true" :model="searchForm" ref="searchForm">
      <el-form-item label="违禁词" prop="contrabandWords">
        <el-input v-model="searchForm.contrabandWords" placeholder="违禁词"></el-input>
      </el-form-item>
      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="queryTableData(1)">查询</el-button>
        <el-button type="primary" @click="handleReset">重置</el-button>
        <el-button type="primary" @click="dialogBoxAddedButton">新增</el-button>
        <el-button type="primary" @click="batchDelete">批量删除</el-button>
        <!-- <el-upload
          ref="upload"
          class="upload-btn-wrap"
          :action="`${uploadBaseURL}sysback/productSearchKeyword/import`"
          :show-file-list="false"
          accept=".xlsx"
          :limit="1"
          :auto-upload="true"
          :before-upload="beforeUpload"
          :on-success="uploadSuccess"
          :on-error="uploadFail"
          :disabled="uploadLoading"
        >
          <el-button :loading="uploadLoading" type="primary">导入</el-button>
        </el-upload> -->
      </el-form-item>
    </el-form>
    <basic-table
      :loading="tableInfo.loading"
      :columns="tableInfo.columns"
      :dataSource="tableInfo.list"
      :pagination="pagination"
      :pageSizes="pageSizes"
      :showSelectCol="true"
      :actionColWidth="100"
      :selectionChange="handleTableSelection"
      @pageChange="handlePageChange"
      @pageSizeChange="handlePageSizeChange"
    >
    </basic-table>
    <contrabandWords-dialog
      ref="contrabandWordsDialog"
      v-on:listenToChildEvent="datafromSon"
    ></contrabandWords-dialog>
  </el-card>
</template>
<script>
import { ApiRequest } from '@api/basePage';
import BasicTable from '@/components/BasicTable';
import { isResponseSuccess } from '@/utils/bizUtils';
import contrabandWordsDialog from './components/contrabandWordsDialog';

export default {
  components: {
    BasicTable,
    contrabandWordsDialog
  },
  data () {
    return {
      // 删除取消提示
      visible: false,
      // 加载提示显示
      uploadLoading: false,
      // 关键字查询
      searchForm: {
        contrabandWords: ''
      },
      // 表格
      tableInfo: {
        list: [], // 表格数据
        columns: [
          {
            key: 'contrabandWords',
            title: '违禁词',
            width: 140
          },
          {
            key: 'createOpeTime',
            title: '添加时间',
            width: 140
          },
          {
            key: 'createUserName',
            title: '添加人',
            width: 140
          },
          {
            key: 'createOrgName',
            title: '添加部门',
            width: 140
          },
        ], // 表格columns
        loading: false // 表格加载loading
      },
      pagination: {
        nowPage: 1, // 当前页码
        totalNum: 0, // 总条数
        pageShow: 10 // 每页条数
      },
      pageSizes: [10, 20, 30, 40], // 每页条数size集合
      addBtnLoading: false // 新增按钮loading
    };
  },
  mounted () {
    // 页面加载初始化表格数据
    this.queryTableData();
  },
  methods: {
    // 新增违禁词
    datafromSon (data) {
      ApiRequest(
        `/sysback/product/contrabandWords/batchSave`,
        'post',
        { addWordsList: data },
        false
      )
        .then((res) => {
          if (!isResponseSuccess(res)) {
            return false;
          }
          this.$message.success('添加成功');
          this.$refs['contrabandWordsDialog'].dialogFormVisible = false;
          this.queryTableData();
        })
        .catch(() => {
          this.tableInfo.loading = false;
        });
    },
    // 重置搜索框
    handleReset () {
      this.$refs.searchForm.resetFields();
    },
    // 对话框新增按钮
    dialogBoxAddedButton () {
      this.$refs['contrabandWordsDialog'].dialogFormVisible = true;
    },
    // 上传成功的回调
    uploadSuccess (file) {
      this.uploadLoading = false;
      this.$refs.upload.clearFiles();
      if (!isResponseSuccess(file)) {
        return this.$message.error(file.retMessage);
      }
      this.$message.success('上传成功');
      this.queryTableData();
    },
    // 上传失败的回调
    uploadFail () {
      this.uploadLoading = false;
      this.$message.error('导入失败!');
    },
    // 上传之前的文件类型验证
    beforeUpload () {
      this.uploadLoading = true;
    },
    // 表格勾选
    handleTableSelection (list) {
      this.tableInfo.selection = list;
    },
    //批量删除
    batchDelete(){
      var list = this.tableInfo.selection;
      if(list.length == 0){
        this.$message.warning('请勾选删除行');
      }
      var uuids = [];
      for(var i=0; i<list.length; i++){
        uuids.push(list[i].uuid);
      }
      ApiRequest(
        '/sysback/product/contrabandWords/batchDelete',
        'post',
        {deleteUuidList:uuids}
      )
        .then((res) => {
          if (!isResponseSuccess(res)) {
            return false;
          }
          this.$message.success('删除成功');
          this.queryTableData();
        })
        .catch(() => {
          this.tableInfo.loading = false;
        });
    },
    // 通过搜索框查询
    queryTableData (pageNum) {
      this.tableInfo.loading = true;
      if (pageNum) {
        this.pagination.nowPage = pageNum;
      }
      ApiRequest(
        '/sysback/product/contrabandWords/pagerList',
        'post',
        { ...this.searchForm, ...this.pagination }
      )
        .then((res) => {
          this.tableInfo.loading = false;
          if (isResponseSuccess(res)) {
            const { results = [], totalNum } = res.retData
            this.pagination.totalNum = totalNum;
            this.tableInfo.list = results;
          }
        })
        .catch(() => {
          this.tableInfo.loading = false;
        });
    },
    /* 改变当前页码事件 */
    handlePageChange (val) {
      this.pagination.nowPage = val;
      this.queryTableData();
    },
    /* 改变每页条数事件 */
    handlePageSizeChange (val) {
      this.pagination.pageShow = val;
      this.queryTableData(1);
    },
  }
};
</script>
<style lang="less" scoped>
.btn-wrap {
  button {
    margin-left: 10px;
    float: left;
  }
  .upload-btn-wrap {
    float: left;
  }
}
</style>
